package com.jinzhi.jzweb.controller.draw;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.domain.TrackDrawVO;
import com.jinzhi.jzweb.service.draw.FuZhouDrawService;
import com.jinzhi.jzweb.service.draw.JiangXiDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/jzweb/jiangXiDraw")
public class JiangXiDrawController {

    @Autowired
    private JiangXiDrawService jiangXiDrawService;

    /**
     * 抽签页面
     * @return
     */
    @GetMapping()
    String TrackDraw(){
        return "jzweb/jiangXiDraw/jiangXiDraw";
    }


    /**
     * 清空抽签结果
     * @return
     */
    @ResponseBody
    @GetMapping("/empty")
    public Result<?> empty(){

        return Result.ok(jiangXiDrawService.delete(new EntityWrapper<TrackDrawDO>()));
    }


    /**
     * 抽签接口
     * @return
     */
    @ResponseBody
    @GetMapping("/schoolDraw")
    public Result<?> draw(){

        return jiangXiDrawService.schoolDraw();
    }

    /**
     * 查询上次的抽签结果
     */
    @ResponseBody
    @GetMapping("/trackList")
    public Result<TrackDrawDO> trackList(){

        return Result.ok(jiangXiDrawService.trackList());
    }



    /**
     * 导出抽签结果信息
     *
     * @param response
     */
    @RequestMapping("/downloadImportTemplateSchool")
    @ResponseBody
    public void downloadImportTemplateSchool(HttpServletRequest request, HttpServletResponse response) {
        Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
        wrapper.orderBy(true,"sort");
        List<TrackDrawDO> trackDrawDOS = jiangXiDrawService.selectList(wrapper);
        List<TrackDrawVO> list = new LinkedList<>();
        if(null!=trackDrawDOS && !trackDrawDOS.isEmpty()){
            for (TrackDrawDO trackDrawDO : trackDrawDOS) {
                TrackDrawVO trackDrawVO = new TrackDrawVO();
                BeanUtils.copyProperties(trackDrawDO,trackDrawVO);
                list.add(trackDrawVO);
            }
        }
        if (list != null && list.size() > 0) {
            //导出操作
            EasyPoiUtil.exportExcel(list, "抽签信息导出", "抽签信息导出", TrackDrawVO.class,  "抽签信息导出", response);
        } else {
            throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
        }
    }



}
